define(['jquery', 'bootstrap', 'backend','frontend', 'form', 'template','highcharts'], function ($, undefined,Backend, Frontend, Form, Template, Highcharts) {
    var Controller = {
        index: function () {
            // console.log(456);
            Controller.getdata();
            $('#u28_input').change(function () {
                $.ajax({
                    url: '/index/appquality/getNames',
                    type:'post',
                    data: {name: $('#u28_input').val()},
                    dataType:'json',
                    success:function(json){
                        if(json.code==403){
                            layer.alert('你没有权限', {icon: 7});
                        }
                        //$("#u30_input").removeAttr('disabled');
                        $("#u30_input").html('<option value="">全部</option>');
                        $.each(json.list,function (index,item) {
                            $("#u30_input").append('<option value="'+item+'">'+item+'</option>');
                        });
                    }
                });
            });
            $('#u30_input').change(function () {
                var type=$('#u28_input').val();
                var name=$('#u30_input').val();
                if(name!=""&&name!=undefined&&type!=""&&type!=undefined){
                    var layerObj = Fast.api.open('/index/appquality/analysis?dialog=1&type='+type+"&name="+name,type+" - "+name);
                    Layer.full(layerObj);
                }else{
                    layer.alert('请选择应用类别和应用名称', {icon: 7});
                }
            });
            $('#u33').click(function () {
                var type=$('#u28_input').val();
                var name=$('#u30_input').val();
                if(name!=""&&name!=undefined&&type!=""&&type!=undefined){
                    var layerObj = Fast.api.open('/index/appquality/analysis?dialog=1&type='+type+"&name="+name,type+" - "+name);
                    Layer.full(layerObj);
                }else{
                    layer.alert('请选择应用类别和应用名称', {icon: 7});
                }
            });
        },
        analysis: function () {
            $('#u28_input').change(function () {
                $.ajax({
                    url: '/index/appquality/index',
                    type:'post',
                    data: {name: $('#u28_input').val()},
                    dataType:'json',
                    success:function(json){
                        //$("#u30_input").removeAttr('disabled');
                        $("#u30_input").html('<option value="">全部</option>');
                        $.each(json,function (index,item) {
                            $("#u30_input").append('<option value="'+item+'">'+item+'</option>');
                        });
                    }
                });
            });
            Controller.delay();
            Controller.shaking();
            Controller.packageLoss();
            //页面要求5分钟刷新一次
            setInterval(function(){
                Controller.delay();
                Controller.shaking();
                Controller.packageLoss();
            }, 900000);
        },
        delay: function () {
            // console.log(456);
            $.ajax({
                url: '/index/appquality/delay',
                type:'post',
                data: {propertyname: 'basedlinkdetails'},
                dataType:'json',
                success:function(json){
                    //console.log(json);
                    Controller.piechart(json, "qualityScore", "delay");
                }
            });
        },
        shaking: function () {
            // console.log(456);
            $.ajax({
                url: '/index/appquality/shaking',
                type:'post',
                data: {propertyname: 'basedlinkdetails'},
                dataType:'json',
                success:function(json){
                    //console.log(json);
                    Controller.piechart(json, "sharkScore", "shaking");
                }
            });
        },
        packageLoss: function () {
            // console.log(456);
            $.ajax({
                url: '/index/appquality/packageLoss',
                type:'post',
                data: {propertyname: 'basedlinkdetails'},
                dataType:'json',
                success:function(json){
                    //console.log(json);
                    Controller.piechart(json, "lossScore", "packageLoss");
                }
            });
        },
        piechart: function(json, type, id){ //画图 饼图/趋势图   数据   类型(画图的字段后期可扩展)
            var toolTipValue="";
            var text = "";
            if(id == 'delay' ){
                text = '时延(ms)';
            }else if(id == 'shaking'){
                text = '抖动(ms)';
            }else if(id == 'packageLoss'){
                text = '丢包率(%)';
            }

            if (type == "qualityScore") {
                text += '';
                toolTipValue = "时延(ms)";
            } else if (type == "sharkScore") {
                text += '';
                toolTipValue = "抖动(ms)";
            } else if(type == "lossScore"){
                text += '';
                toolTipValue = "丢包率(%)";
            }

            if(json.code == 401){
                Backend.api.toastr.error(json.msg);
                window.location.href='/index/user/login';
                return false;
            }

            if(json.code == 0 || json.code == 403){
                // alert(msg);
                options = {
                    positionClass: "toast-top-center",
                };
                Backend.api.toastr.error(text+json.msg,'',options);
                return false;
            }
            // console.log(json.xAxis);
            // console.log(json.series);
            Highcharts.chart(id, {
                chart: {
                    // zoomType: 'x',
                    // type: 'area'
                },
                title: {
                    style: {
                        // color: 'red',   //字体颜色
                        fontSize:"14px", //字体大小
                        "font-weight": "bold",
                    },
                    text: text,
                    style: {
                        // color: 'red',   //字体颜色
                        fontSize:"14px", //字体大小
                        "font-weight": "bold",
                    },
                    x: -20 //center
                },

                credits: {
                    enabled: false
                },
                xAxis: {
                    tickInterval:10,//刻度的宽度,
                    categories : json.xAxis,
                    labels:{
                        x:45
                    },
                },
                yAxis: {
                    title: {
                        text: null,
                    },
                    labels: {
                        formatter : function(){//设置纵坐标的样式
                            if(type=="001"&&id=="blankScreen"){
                                if(this.value=="0.00"){
                                    return "否";
                                }else{
                                    return "是";
                                }
                            }else{
                                return this.value;
                            }
                        }
                    },
                    plotLines: [{
                        value: 0,
                        width: 1,
                        color: '#808080'
                    }],
                    // min:0,
                    // tickInterval:20,//刻度的宽度
                },
                legend: {
                     enabled: false   //隐藏图例
                },
                tooltip : {
                    formatter : function() {
                        if(type=="001"&&id=="blankScreen"){
                            if(this.point.y=="0.00"){
                                return '<b>' + '</b><br>时间:' + this.key + '<br>'+toolTipValue+'：' + "否";
                            }else{
                                return '<b>'+ '</b><br>时间:' + this.key + '<br>'+toolTipValue+'：' + "是";
                            }
                        }
                        else
                            return '<b>'+ '</b><br>时间:' + this.key + '<br>'+toolTipValue+'：' + this.point.y;
                    }
                },
                // series:[{
                //     seriesshowInLegend: false,   //隐藏数据名称
                //     type :'area',
                //     // name:'网信',
                //     data:json.series
                // }]
                series:json.series
            });
        },


        //渲染矩形和数据
        getdata: function () {
            $.ajax({
                url: '/index/appquality/main_app',
                type:'post',
                dataType:'json',
                success:function(json){
                    var data = JSON.parse(json);
                    console.log(data);
                    if(data.code == 401){
                        Backend.api.toastr.error(data.msg);
                        return false;
                    }
                    if(data.code == 0 || data.code == 403){
                        // alert(msg);
                        options = {
                            positionClass: "toast-top-center",
                        };
                        Backend.api.toastr.error(data.msg,'',options);
                        return false;
                    }
                    //获取div向内填充数据
                    //var length = Object.keys(data.data);
                    for (var index in data.data){
                        //console.log(index);
                        var list = data.data[index].list
                        if(data.data[index].name != '游戏'){
                            if(list != undefined){
                                if(list.length > 0 ){
                                    $('.app').append(
                                        '<div class="u49" id="'+index+'">'+
                                        '<div class="u55">'+
                                        '<p><span>'+data.data[index].name+'</span></p>'+
                                        '</div>'+
                                        '<div class="u54">'+
                                        '<img class="u54_img" class="img" src="'+data.data[index].icon+'">'+
                                        '</div>'+
                                        '</div>'
                                        );
        
                                        for(var i in list){
                                            var level = list[i].level
                                                img  =  ''
                                                switch (level) {
                                                    case '好':
                                                            img = '/assets/img/frontend/app_quality/u60.png'
                                                        break;
                                                    case '较好':
                                                            img = '/assets/img/frontend/app_quality/u61.png'
                                                        break;
                                                    case '警告':
                                                            img = '/assets/img/frontend/app_quality/u77.png'
                                                        break;
                                                    case '较差':
                                                            img = '/assets/img/frontend/app_quality/u94.png'
                                                        break;
                                                    default:
                                                            img = '/assets/img/frontend/app_quality/u110.png'
                                                        break;
                                                }
                                            $('#'+index).append(
                                                '<ul>'+
                                                '<li title="'+list[i].name+'">'+list[i].name+'</li>'+
                                                '<li>'+
                                                '<img src="'+img+'">'+
                                                '<span style="font-size: 12px">'+list[i].value+'ms'+'</span>'+
                                                '</li>'+
                                                '</ul>'
                                            )
                                        }
                                }
                            }
                        }


                        
                        //var list = data.data[index].list
                        
                    }
                }
            });
        }


    };
    return Controller;
});